<template>
	<view>
		<image class="bg" :src="image" mode="widthFix"></image>
		<view class="container">
			<view class="submitBtn" @click="show">
				Invite Friends now
				<image src="@/static/image/InviteIcon.png" mode=""></image>
			</view>
			<view class="list">
				<view class="col" v-for="(item,index) in desc" :key="index">
					<view class="index">
						{{index+1}}
					</view>
					<view class="text">
						{{item}}
					</view>
				</view>
			</view>
		</view>
		<uni-popup ref="popup">
			<view class="popup">
				<view class="content">
					<image :src="invite_poster" mode="widthFix"></image>
				</view>
				<view class="btns">
					<view class="btn btn1" @click="saveImg">
						Save image
					</view>
					<view class="btn btn2" @click="copy(invite_code_url)">
						Copy referral link
					</view>
				</view>
				<uni-icons type="close" size="40" color="#fff" @click="close"></uni-icons>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				image:'',
				desc:[],
				domain:'',
				invite_code:'',
				invite_code_url:'',
				invite_poster:'',
			}
		},
		onShow() {
			const token = uni.getStorageSync("token")
			if (!token) {
				uni.reLaunch({
					url: "/pages/landing"
				})
				return
			}
			let site = uni.getStorageSync('site')
			this.image = site.invite_image
			this.desc = site.invite_desc
			this.domain = site.domain
			this.getData()
		},
		methods: {
			getData() {
				this.$post("/user/get_invite").then(res => {
					// console.log(res)
					this.invite_code = res.data.invite_code
					this.invite_code_url = res.data.invite_code_url
					this.invite_poster = res.data.invite_poster
			
				})
			},
			show() {
				this.$refs.popup.open()
			},
			close() {
				this.$refs.popup.close()
			},
			copy(text) {
				const _this = this
				uni.setClipboardData({
					data: text,
					success: function() {
						_this.$toast("Copy successfully!")
					}
				});
			},
			// 保存图片到相册
			saveImg() {
				// 创建a标签并模拟点击下载
				let link = document.createElement('a');
				link.href = this.invite_poster;
				link.download = 'FFFcards.png'; // 设置下载文件名
				document.body.appendChild(link);
				link.click();
				document.body.removeChild(link);
			}

		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #EDF0FF;
	}

	.container {
		padding: 60rpx;
	}

	.submitBtn {
		font-size: 40rpx;
		position: relative;

		image {
			position: absolute;
			width: 64rpx;
			height: 64rpx;
			right: 20rpx;
			bottom: -24rpx;
		}
	}

	.list {
		margin-top: 40rpx;

		.col {
			padding: 20rpx 0;
			display: flex;

			.index {
				width: 30rpx;
				height: 30rpx;
				background: #63D7DA;
				border-radius: 8rpx;
				line-height: 30rpx;
				font-size: 22rpx;
				text-align: center;
				font-weight: bold;
				color: #fff;
			}

			.text {
				width: calc(100% - 30rpx);
				padding-left: 20rpx;
				font-size: 26rpx;
			}
		}
	}

	.popup {
		width: 100vw;
		padding: 0 32rpx;

		.content {
			background-color: #fff;
			border-radius: 8rpx;
			overflow: hidden;
			margin-bottom: 20rpx;
		}

		.btns {
			display: flex;
			justify-content: space-between;

			.btn {
				width: calc(50% - 10rpx);
				border-radius: 8rpx;
				color: #fff;
				font-weight: bold;
				line-height: 114rpx;
				height: 114rpx;
				font-size: 32rpx;
				text-align: center;
			}

			.btn1 {
				background: #63D7DA;
			}

			.btn2 {
				background: #0C122A;
			}
		}

		.uni-icons {
			margin: 0 auto;
			display: block;
		}
	}
</style>